#!/bin/sh /etc/rc.common

START=97

ENABLED=0
WORKGROUPD=0
IOCHARSET=0
GUEST=""
USERS=""
AGM=""

cifs_header() {
	local enabled
	local workgroup 

	config_get enabled $1 enabled
	config_get workgroup $1 workgroup

	ENABLED=$enabled
	WORKGROUPD=$workgroup
}

mount_natshare() {
	local server
	local name
	local natpath
	local guest
	local users
	local pwd
	local agm
	local iocharset
	local smbver

	config_get server $1 server
	config_get name $1 name
	config_get natpath $1 natpath
	config_get guest $1 guest
	config_get users $1 users
	config_get pwd $1 pwd
	config_get agm $1 agm
	config_get iocharset $1 iocharset
	config_get smbver $1 smbver
	
	mkdir -p $natpath && chmod 777 $natpath
	if [ "$smbver" = "nil" ];then
		smbverarg=""
	else
		smbverarg="vers=$smbver,"
	fi
	#echo "mount -t cifs -o ${smbverarg}username=$users,password=$pwd,iocharset=$iocharset,$agm //$server/$name $natpath"
	/usr/bin/mount -t cifs -o ${smbverarg}username=$users,password=$pwd,iocharset=$iocharset,$agm //$server/$name $natpath
}

mount_davfs() {
	local url
	local username
	local password
	local mountpoint
	local arguments

	config_get url $1 url
	config_get username $1 username
	config_get password $1 password
	config_get mountpoint $1 mountpoint
	config_get arguments $1 arguments

	if [ -z "$url" -o -z "$mountpoint" ]; then
		logger -t davfs "url or mountpoint is empty"
		return 1
	fi
	mkdir -p $mountpoint && chmod 777 $mountpoint

	[ -n "$arguments" ] && arguments=",$arguments"

	{ echo "$username"; echo "$password"; } | /usr/bin/mount -t davfs -o conf=/etc/davfs2/script.conf$arguments $url $mountpoint
}

start() {
	config_load cifs
	config_foreach cifs_header cifs

	if [ "$ENABLED" == 1 ]; then
		config_foreach mount_natshare natshare
		#echo "Cifs Mount succeed."
		config_foreach mount_davfs davfs
	fi
}

stop() {
	/usr/bin/mount | grep '//' | awk -F ' ' '{print $3}' | while read line; do
		#echo "umount -d -l $line"
		/usr/bin/umount -d -l $line 2>/dev/null
	done
}

